#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 1.Module documentation
"""
    This script will download NDS_List_Japan.zip from it's official site,
    and insert the data into OnlineList DB.
    Reference
      URL : http://ndslist.hp.infoseek.co.jp/NDS_List_Japan.zip
      OnlineList DB : reference to models.py
"""
"""
  Copyright 2009  Thomas Tsui (email : thomas+onlinelist@jbobos.org)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

# 2.Module imports
import sys, os, codecs, re, logging
import xmlrpclib, urllib, urllib2
from xml.dom import minidom
from onlinelist.models import NDSSource, NDSGame

# 3.Global variables declarations
VERSION = "0.0.1"

# 4.Class declarations

# 5.Function declarations
def fetchEntry(url):
    sock = openAnything(url)
    xmldoc = minidom.parse(sock).documentElement
    sock.close()
    logging.debug(xmldoc.getElementsByTagName('datName')[0].firstChild.data)

    #ns = NDSSource(
    #  datName = xmldoc.getElementsByTagName('datName')[0].firstChild.data,
    #  datVersion = xmldoc.getElementsByTagName('datVersion')[0].firstChild.data,
    #  system = xmldoc.getElementsByTagName('system')[0].firstChild.data,
    #  language = 1,
    #  datVersionURL = "http://ndslist.hp.infoseek.co.jp/ver.txt",
    #  datURL = "http://ndslist.hp.infoseek.co.jp/NDS_List_Japan.zip",
    #  imURL = "http://www.retrocovers.com/offline/imgs/ADVANsCEne_NDS/",
    #)
    # ns.save()

    gamesNodes = xmldoc.getElementsByTagName('games')[0]
    games = [e for e in gamesNodes.childNodes if e.nodeType == e.ELEMENT_NODE]
    # print games[0].toxml()
    for game in games :
        #print game.getElementsByTagName('title')[0].firstChild.data
        try :
            ns_game = NDSGame(
                imageNumber = game.getElementsByTagName('imageNumber')[0].firstChild.data,
                releaseNumber = game.getElementsByTagName('releaseNumber')[0].firstChild.data,
                title = game.getElementsByTagName('title')[0].firstChild.data,
                saveType = game.getElementsByTagName('saveType')[0].firstChild.data,
                romSize = game.getElementsByTagName('romSize')[0].firstChild.data,
                publisher = game.getElementsByTagName('publisher')[0].firstChild.data,
                location = game.getElementsByTagName('location')[0].firstChild.data,
                sourceRom = game.getElementsByTagName('sourceRom')[0].firstChild.data,
                language = game.getElementsByTagName('language')[0].firstChild.data,
                romCRC = game.getElementsByTagName('romCRC')[0].firstChild.data,
                im1CRC = game.getElementsByTagName('im1CRC')[0].firstChild.data,
                im2CRC = game.getElementsByTagName('im2CRC')[0].firstChild.data,
                comment = ""
            )
            ns_game.save()
        except :
            print game.getElementsByTagName('title')[0].firstChild.data

def download_and_unzip():
    pass

def getText(node):
    rc = ""
    if node.nodeType == node.TEXT_NODE:
        rc = rc + node.data
    return rc

def openAnything(source):
    """URI, filename, or string --> stream

    This function lets you define parsers that take any input source
    (URL, pathname to local or network file, or actual data as a string)
    and deal with it in a uniform manner.  Returned object is guaranteed
    to have all the basic stdio read methods (read, readline, readlines).
    Just .close() the object when you're done with it.

    Examples:
    >>> from xml.dom import minidom
    >>> sock = openAnything("http://localhost/kant.xml")
    >>> doc = minidom.parse(sock)
    >>> sock.close()
    >>> sock = openAnything("c:\\inetpub\\wwwroot\\kant.xml")
    >>> doc = minidom.parse(sock)
    >>> sock.close()
    >>> sock = openAnything("<ref id='conjunction'><text>and</text><text>or</text></ref>")
    >>> doc = minidom.parse(sock)
    >>> sock.close()
    """
    if hasattr(source, "read"):
        return source

    if source == '-':
        import sys
        return sys.stdin

    # try to open with urllib (if source is http, ftp, or file URL)
    import urllib
    try:
        return urllib.urlopen(source)
    except (IOError, OSError):
        pass

    # try to open with native open function (if source is pathname)
    try:
        return open(source)
    except (IOError, OSError):
        pass

    # treat source as string
    import StringIO
    return StringIO.StringIO(str(source))

def main():
    # url = "http://ndslist.hp.infoseek.co.jp/NDS_List_Japan.zip"
    url = "NDS_List_Japan.xml"
    fetchEntry(url)

# 6.Main body
if __name__ == "__main__" :
    logging.basicConfig(level    = logging.DEBUG,
                        format   = 'LINE %(lineno)-4d  %(levelname)-8s %(message)s',
                        datefmt  = '%m-%d %H:%M',
                        filename = 'updatedb.log',
                        filemode = 'w');
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    # set a format which is simpler for console use
    formatter = logging.Formatter('LINE %(lineno)-4d : %(levelname)-8s %(message)s')
    # tell the handler to use this format
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    try:
        main()
    except:
        logging.exception("Unexpected error")
        raise
